<?php

/**
 * this class help to parse email content var and send mail directly using email function or smtp mail base on phpmailer
 */
class EmailHelper {

    /**
     * send email using newsletter template
     * @param string $receiver receiver email
     */
    public static function sendNewsletter($receiver) {

    }

    /**
     * send mail after custommer click submit contact form
     * @param mix $receiptContent array or object,
     * array('subject' => 'message', 'body' => 'body', 'email' => email, 'phone' => 'phone')
     */
    public static function sendContactRequest($receiptContent) {
        //get email template
        $mailTemplate = db()->createCommand()
                ->select('*')
                ->from('{{email_templates}}')
                ->where('id = 10')
                ->queryRow();

        if (is_array($receiptContent)) {
            $body = str_replace(array('[MESSAGE]', '[SENDER]', '[NAME]', '[PHONE]', '[MAILSUBJECT]', '[IP]', '[SITE_NAME]', '[URL]'),
                    array(
                        $receiptContent['body'],
                        $receiptContent['email'],
                        $receiptContent['name'],
                        $receiptContent['phone'],
                        $receiptContent['subject'],
                        $_SERVER['REMOTE_ADDR'],
                        'Contact',
                        bu()
                    ),
                    $mailTemplate['body']
            );
        } elseif (is_object($receiptContent)) {
            $body = str_replace(array('[MESSAGE]', '[SENDER]', '[NAME]', '[PHONE]', '[MAILSUBJECT]', '[IP]', '[SITE_NAME]', '[URL]'),
                    array(
                        $receiptContent->body,
                        $receiptContent->email,
                        $receiptContent->name,
                        $receiptContent->phone,
                        $receiptContent->subject,
                        $_SERVER['REMOTE_ADDR'],
                        'Contact',
                        bu()
                    ),
                    $mailTemplate['body']
            );
        } else {
            //model not valid
            throw new CHttpException(500, 'receiver MUST be CFormModel or array');
            return;
        }

        //send mail to admin
        $adminEmail = app()->settings->get('site', 'admin_email');
        self::send($adminEmail, $receiver['subject'], $body);
    }

    /**
     * send mail to reveiver
     * @param string $receiver email
     * @param string $subject
     * @param string $body
     */
    public static function send($receipt, $subject, $body = '', $recipientName = '') {
        //reading config
        $setting = app()->settings;
        $emailConfig = $setting->load('email');
        $adminEmail = $setting->get('site', 'admin_email');

        if ($emailConfig['smtp']) { //using smtp email
            //load phpmailer class
            Yii::import('common.extensions.swiftMailer.SwiftMailer');

            $mailer = new SwiftMailer;
            // Get config
            $mailHost = $emailConfig['smtp_host'];
            $mailPort = $emailConfig['smtp_port']; // Optional
            // New transport
            $Transport = $mailer->smtpTransport($mailHost, $mailPort);

            // Mailer
            $Mailer = $mailer->mailer($Transport);

            // New message
            $Message = $mailer
                    ->setContentType('text/html')
                    ->newMessage($subject)
                    ->setFrom(array($adminEmail => $adminEmail))
                    ->setTo(array($receipt => $recipientName))
                    //->addPart($content, 'text/html')
                    ->setBody($body);

            // Send mail
            $Mailer->send($Message);
        } else { //using email() function
            $headers = "From: {$adminEmail}\r\nReply-To: {$adminEmail}";
            mail($receipt, $subject, $body, $headers);
        }
    }

}